package com.zzr.demo.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicTemplateMessageIndustryModifyRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayOpenPublicTemplateMessageIndustryModifyResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.zzr.demo.alipay.config.AliPayCertConfig;

/**
 * @author: xiaomi
 * @date: 2021/8/31
 * @description:
 */
public class PayTest2 {
    private static String AliPay_CERT_FILE_PATH = "D:/Projects/IdeaProjects/Projections/Demos/springboot-demos/alipay-demo/src/main/resources/static/";

    public static void main(String[] args) throws AlipayApiException {
        cert();

    }

    /**
     * 普通公钥电脑端下订单
     * 1. pageExecute 适用 PC和手机网站：
     * 如果在电脑端下订单，支付宝服务端会返回一段脚本，浏览器打开会直接跳转到支付宝的收银台！
     * 2. execute：API 调用，木有页面输出
     * 3. SdkExecute：APP服务端的
     */
    public static void normal() throws AlipayApiException {
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayCertConfig.SERVERURL, AliPayCertConfig.APPID
                , AliPayCertConfig.APP_PRIVATE_KEY, "json", AliPayCertConfig.CHARSET, AliPayCertConfig.ALIPAY_PUBLIC_KEY, "RSA2");
//        实例化具体API对应的request类, 类名称和接口名称对应, 当前调用接口名称：alipay.open. public.template.message.industry.modify
        AlipayOpenPublicTemplateMessageIndustryModifyRequest request = new AlipayOpenPublicTemplateMessageIndustryModifyRequest();
//SDK已经封装掉了公共参数，这里只需要传入业务参数
//此次只是参数展示，未进行字符串转义，实际情况下请转义
        request.setBizContent("  {" +
                "    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
                "    \"primary_industry_code\":\"10001/20102\"," +
                "    \"secondary_industry_code\":\"10001/20102\"," +
                "    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
                " }");

//        普通公钥
        AlipayOpenPublicTemplateMessageIndustryModifyResponse response = alipayClient.execute(request);
//调用成功，则处理业务逻辑
        if (response.isSuccess()) {
            //.....
        }
    }

    public static void cert() throws AlipayApiException {
        //构造client
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
//设置网关地址
        certAlipayRequest.setServerUrl(AliPayCertConfig.SERVERURL);
//设置应用Id
        certAlipayRequest.setAppId(AliPayCertConfig.APPID);
//设置应用私钥
        certAlipayRequest.setPrivateKey(AliPayCertConfig.APP_PRIVATE_KEY);
//设置请求格式，固定值json
        certAlipayRequest.setFormat("json");
//设置字符集
        certAlipayRequest.setCharset(AliPayCertConfig.CHARSET);
//设置签名类型
        certAlipayRequest.setSignType(AliPayCertConfig.SIGN_TYPE);
//设置应用公钥证书路径
        certAlipayRequest.setCertPath(AliPay_CERT_FILE_PATH + AliPayCertConfig.app_cert_path);
//设置支付宝公钥证书路径
        certAlipayRequest.setAlipayPublicCertPath(AliPay_CERT_FILE_PATH + AliPayCertConfig.alipay_cert_path);
//设置支付宝根证书路径
        certAlipayRequest.setRootCertPath(AliPay_CERT_FILE_PATH + AliPayCertConfig.alipay_root_cert_path);
//构造client
        AlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
//构造API请求
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("  {" +
                "    \"primary_industry_name\":\"IT科技/IT软件与服务\"," +
                "    \"primary_industry_code\":\"10001/20102\"," +
                "    \"out_trade_no\":\"abc10001\"," +
                "    \"secondary_industry_name\":\"IT科技/IT软件与服务\"" +
                " }");
//发送请求
        AlipayTradeQueryResponse response = alipayClient.certificateExecute(request);
        if (response.isSuccess()) {
            //.....
        }
        //参数无效：支付宝订单号和外部订单号不能同时为空!
    }
}
